package co.za.itweb.itwebpro;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class TwitterWebviewActivity extends Activity {
	private Intent mIntent;
	
	public void onCreate(Bundle savedInstanceState)
	{
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.twitter_webview);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("If you don't have Twitter account you will have to sign up and then confirm link in your recieved email to be able to tweet.If you already " +
				"have an account just log in and your tweet will be processed.")
		       .setCancelable(false)
		       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   dialog.cancel();
		           }
		       
		       
		       });
		AlertDialog alert = builder.create();
		alert.show();
		mIntent = getIntent();
		String url = (String)mIntent.getExtras().get("URL");
		
		WebView webView = (WebView) findViewById(R.id.webview);
		webView.setWebViewClient( new WebViewClient()
		{
			
	
			
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url)
			{
				if( url.contains(getResources().getString( R.string.twitter_callback )))
				{
					Uri uri = Uri.parse( url );
					String oauthVerifier = uri.getQueryParameter( "oauth_verifier" );
					mIntent.putExtra( "oauth_verifier", oauthVerifier );
					setResult( RESULT_OK, mIntent );
					
					finish();
					return true;
				}
				return false;
			}
		});
		webView.loadUrl(url);
		
		
		 Button buttonclose = (Button) findViewById(R.id.closetwitter);
			buttonclose.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {

					TwitterWebviewActivity.this.finish();
				}
			});
	}
	
	
	@Override
	public void onConfigurationChanged (Configuration newConfig){
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	    if ((keyCode == KeyEvent.KEYCODE_BACK))
	    {
	    	
	    	
	    	
	    		
	    		TwitterWebviewActivity.this.finish();


	    	
	    }
	    return super.onKeyDown(keyCode, event);
	}
}
